// Copyright (C) 2025 woodycpp
//
// This work is licensed under the Creative Commons Attribution-ShareAlike 4.0
// International License. To view a copy of this license, visit
// https://creativecommons.org/licenses/by-sa/4.0/
//
// You are free to:
//   * Share — copy and redistribute the material in any medium or format
//   * Adapt — remix, transform, and build upon the material
//
// Under the following terms:
//   * Attribution — You must give appropriate credit, provide a link to the
//   license,
//     and indicate if changes were made. You may do so in any reasonable
//     manner, but not in any way that suggests the licensor endorses you or
//     your use.
//   * ShareAlike — If you remix, transform, or build upon the material, you
//   must
//     distribute your contributions under the same license as the original.
//
// In short: You must credit the original author (woodycpp, 2025) and share any
// derivative works under the same CC-BY-SA-4.0 license.

#include "cal_mul_max.h"

void calMulMax(int A[], int res[], int n) {
    if (n <= 0) {
        return;
    }
    // 初始化
    int maxRight = A[n - 1];  // 记录当前位置到末尾的最大值
    int minRight = A[n - 1];  // 记录当前位置到末尾的最小值
    res[n - 1] = A[n - 1] * A[n - 1];
    // 依次遍历
    for (int i = n - 2; i >= 0; --i) {
        maxRight = max(A[i], maxRight);  // 更新当前位置到末尾的最大值
        minRight = min(A[i], minRight);  // 更新当前位置到末尾的最小值
        if (A[i] >= 0) {
            res[i] = A[i] * maxRight;  // 正数乘大数结果更大
        } else {
            res[i] = A[i] * minRight;  // 负数乘小数结果更大
        }
    }
}
